preserve

drop if filer == 0
drop if nonRecon == 1
drop if mars == 3

keep if flpdyr == 2019
capture drop temp*
gen temp = exch_income
capture drop xtile*

gen tempFPL = hincpl
replace tempFPL = 401 if hincpl > 400
bysort flpdyr: gen xtile = floor(tempFPL / 50)
replace xtile = xtile - 1 if tempFPL == 400

gen iSafeHarbor = (safeHarbor > 0)
gen iCondSH = (safeHarbor > 0 & APTC_repay > 0)
gen iRepay = (APTC_repay > 0)
gen iTrueUp = (trueUp > 0)

replace iCondSH = . if iRepay == 0 & nonRecon == 0
collapse (mean) iSafeHarbor iRepay iTrueUp iCondSH [w = wt], by(xtile) fast

gen agi_bin = ""
local i = 1
foreach bin in "0-49" "50-99" "100-149" "150-199" "200-249" "250-300" "300-350" "350-400" "400+" {
	replace agi_bin = "`bin'" if xtile == `i'
	local i = `i' + 1
}

order xtile iSafeHarbor iRepay iTrueUp iCondSH agi_bin
sort xtile
keep xtile iSafeHarbor iRepay iTrueUp iCondSH agi_bin

mkmat xtile iSafeHarbor iRepay iTrueUp iCondSH, matrix(output)
putexcel set "$workingDir/output/Figure_4/Figure_4_PTC_comp_distro.xlsx", sheet("reconcile") modify
putexcel A1 = matrix(output), colnames
putexcel close

restore
